---@class InvasionBossItem : UIComponentBase
---@field New fun():InvasionBossItem
local InvasionBossItem = Class("InvasionBossItem",UIComponentBase)
local t = InvasionBossItem
function t:InitComponent(bossNameText,bossLevelText,bossBloodBar,bossHpText,bossGraph,bossBg,skillBtnList, bossMaxController)
    self.bossNameText = bossNameText
    self.bossLevelText = bossLevelText
    self.bossBloodBar = bossBloodBar
    self.bossHpText = bossHpText
    self.bossMaxController = bossMaxController
    ---@type GGraph
    self.bossGraph = bossGraph
    self.bossBg = bossBg
    ---@type Spine.AnimationState
    self.bossAnimState = nil
    self.actionIdleName = "idle1"
    self.actionName = ""
    ---@type GoWrapper
    self.bossWrapper = GoWrapper.New()
    ---@type GameObject
    self.bossDraw = nil
    self.isDestroy = false
    self.skillBtnList = skillBtnList
    self.bossBgName = ""
    self.originX = self.bossGraph.x
    self.originY = self.bossGraph.y
end
function t:OnDestroy()
    self.isDestroy = true
    if self.bossDraw then
        destroy(self.bossDraw)
    end
    if self.bossWrapper then
        self.bossWrapper:Dispose()
    end
    if self.bossBgName ~= "" then
        resMgr:DestroySprite(self.bossBgName)
        self.bossBgName = ""
    end
    self.bossDraw = nil
    self.bossWrapper = nil
    self:DelReferenceAll()
end
function t:LoadBossModel(modelId)
    local cfg = dataTable.getDataTableByStringId("t_herospecial",modelId)
    if not cfg or cfg.f_HeroID == 0 then
        return
    end
    local resName = logicMgr.HeroManager.GetHeroDrawingPath(cfg.f_HeroID)
    if resName == self.resName then
        return
    end
    local offset = cfg.f_HeroMove
    self.resName = resName
    resMgr:LoadModelPrefab(self.resName, function(go)
        if self.isDestroy then
            return
        end
        if self.bossDraw ~= nil then
            destroy(self.bossDraw);
            self.bossDraw = nil
        end
        local model = newObject(go);
        model.name = self.resName;
        model:SetActive(true);
        self.bossDraw = model
        model:AddComponent(TCanvas);
        self.bossWrapper:SetWrapTarget(model, false)
        self.bossGraph:SetNativeObject(self.bossWrapper)
        if string.IsNilOrEmpty(offset) then
            self.bossGraph:SetXY(self.originX,self.originY)
        else
            self.bossGraph:SetXY(self.originX+offset[1],self.originY + offset[2])
        end
        Util.Lua_SetLocalPositionXYZ(model,0, 0,0)
        local scale = cfg.f_HeroSize/10000
        Util.Lua_SetLocalScaleXYZ(model,scale,scale,1)
        ---@type Spine.Unity.SkeletonGraphic
        local skeleton = model:GetComponent("SkeletonGraphic")
        self.bossAnimState = skeleton.AnimationState
        --self.bone = skeleton.Skeleton:FindBone("body_drop")
        --if bone then
            --local x,y = Util.FGUILocalToScreen(self.bossGraph.parent,self.bossGraph.x+bone.X,self.bossGraph.y+bone.Y,0,0)
            --x,y = TBS.Battle.ScreenToScenePoint(x,y,0)
            --TBS.Battle.SetBossPosition(x,y)
        --else
       --     logError("立绘找不到骨骼节点：body_drop")
       -- end
        self:PlayAnim(self.actionName,true)
    end, true);
    if self.bossBgName ~= cfg.f_HeroBg then
        resMgr:DestroySprite(self.bossBgName)
        self.bossBgName = cfg.f_HeroBg
        if cfg.f_HeroBg ~= "" then
            resMgr:LoadSpritePrefab(cfg.f_HeroBg,function(sp)
                self.bossBg.texture = NTexture.New(sp.texture);
            end);
        end
    end

end
function t:InitBoss(modelId,level)
    local cfg = dataTable.getDataTableByStringId("t_herospecial",modelId)
    if not cfg then
        printError("InitBoss 找不到t_herospecial "..modelId)
        return
    end
    if G.dataTable["t_collocationreward"][tostring(level + 1)] then
        self.bossMaxController.selectedIndex = 0;
    else
        self.bossMaxController.selectedIndex = 1;
    end
    local hCfg = dataTable.getDataTableByStringId("t_hero",cfg.f_HeroID)
    self.bossNameText.text = getLanguage(hCfg.f_HeroName)
    self.bossLevelText.text = string.format(getLanguage("Level"),level)
    self.bossBloodBar.value = 100
    self.bossHpText.text = ""
    self.actionIdleName = cfg.f_WaitAction or ""
    self.actionName = self.actionIdleName
    self:LoadBossModel(modelId)
    self:InitSkill(modelId)
end
function t:InitSkill(modelId)
    if not self.skillBtnList then return end
    local cfg = dataTable.getDataTableByStringId("t_herospecial",modelId)
    if not cfg then
        return
    end
    local tb = TablePool.Get()
    table.insert(tb,cfg.f_AtkId)
    table.insert(tb,cfg.f_SkillId)
    table.insert(tb,cfg.f_SkillId2)
    for k,v in pairs(self.skillBtnList) do
        --local lv = self:GetChild("num",v)
        local icon = self:GetChild("Mask/Icon",v)
        local ctrlState = self:GetController("state",v)
        local skillId = tb[k]
        local skillCfg = dataTable.getDataTableByStringId("t_collocationskillshow",skillId)
        if skillCfg then
            ctrlState.selectedIndex = 1
            self:SetIcon(icon,skillCfg.f_SkillIicon,EnumConst.ItemShowType.SKILL)
        else
            ctrlState.selectedIndex = 0
        end

    end
    TablePool.ReleaseDeeper(tb,false)
end
---施法
function t:ReleaseAction(skillId)
    local boss = TBS.ActorMgr.GetBoss()
    local skillCfg = dataTable.getDataTableByStringId("t_collocationskill",skillId)
    if not skillCfg then return end
    local cfg = dataTable.getDataTableByStringId("t_herospecial",boss:GetModelId())
    if not cfg then
        return
    end
    if skillCfg.f_TriggerType == 1 then
        self.actionName = cfg.f_OverhitAction
    else
        self.actionName = cfg.f_ReleaseAction
    end
    if self.actionName == "" then return end
    if not self.bossAnimState then return end
    self:PlayAnim(self.actionName,true)
end
function t:UpdateHP()
    local actor = TBS.ActorMgr.GetBoss()
    local hp = actor:GetHp()
    local hpMax = actor:GetHpMax()
    self.bossHpText.text = string.format("%d/%d",hp,hpMax)
    self.bossBloodBar.value = hp/hpMax*100
end
---受击
function t:HitAction()
    self:UpdateHP()
    local actor = TBS.ActorMgr.GetBoss()
    local cfg = dataTable.getDataTableByStringId("t_herospecial",actor:GetModelId())
    if not cfg or cfg.f_HitAction == "" then
        return
    end
    self.actionName = cfg.f_HitAction
    if not self.bossAnimState then return end
    self:PlayAnim(self.actionName,true)
end
function t:PlayAnim(name,autoIdle)
    if not self.bossAnimState then return end
    local anim =  self.bossAnimState.Data.SkeletonData:FindAnimation(name)
    if anim == nil then
        logError("Animation not found animationName: " ..name)
        return
    end
    self.bossAnimState:SetAnimation(0,name, false)
    if autoIdle and self.actionIdleName ~= "" then
        self.bossAnimState:AddAnimation(0, self.actionIdleName,true,0);
    end
end

function t:GetWPos()
    local x,y = Util.FGUILocalToScreen(self.bossGraph.parent,self.bossGraph.x,self.bossGraph.y,0,0)
    x,y = TBS.Battle.ScreenToScenePoint(x,y,0)
    return x,y
end
return t